﻿using System;
using AllInOnePluginBL.Helpers;
using PluginDB;

namespace AllInOnePluginBL
{
    /// <summary>
    /// Summary description for FavouritsHandler
    /// </summary>
    public class FavouritsHandler
    {
        private readonly PluginDBHandler pluginDBHandler = PluginDBHandler.Instance;

        public bool IsUserExists(long userId)
        {
            return pluginDBHandler.IsUserExists(userId);
        }

        public bool IsUserExists(string userUniqeGuid)
        {
            return pluginDBHandler.IsUserExists(userUniqeGuid);
        }

        public bool IsUserExists(string userName, string password)
        {
            return pluginDBHandler.IsUserExists(userName, password);
        }

        public User GetUser(long userId)
        {
            return pluginDBHandler.GetUser(userId);
        }

        public User GetUser(string userName, string password)
        {
            return pluginDBHandler.GetUser(userName, password);
        }

        public User GetUser(string userGuid)
        {
            return pluginDBHandler.GetUser(userGuid);
        }

        public User CreateUser(string userName, string password)
        {
            return pluginDBHandler.CreateUser(userName, password);
        }

        public bool AddSeriesToFavourits(User user, long seriesId)
        {
            if(!IsUserExists(user.UserID))
                throw new Exception("User not exists");

            if(pluginDBHandler.IsUserSeriesExists(user.UserID, seriesId))
                return false;

            var series = pluginDBHandler.GetSeries(seriesId);

            pluginDBHandler.AddSeriesToUser(user.UserID, series);
            return true;
        }

        public void DeleteSeriesForUser(long userId, long seriesId)
        {
            if (!IsUserExists(userId))
            {
                throw new Exception("User not exists");
            }

            pluginDBHandler.RemoveSeriesForUser(userId, seriesId);
        }
        
        public bool IsSeriesExists(long seriesId)
        {
            return pluginDBHandler.IsSeriesExists(seriesId);
        }

        public bool RemoveSeriesFromFavourits(User user, long seriesId)
        {
            if (!IsUserExists(user.UserID))
                throw new Exception("User not exists");

            if (!pluginDBHandler.IsUserSeriesExists(user.UserID, seriesId))
                return false;

            pluginDBHandler.RemoveSeriesForUser(user.UserID, seriesId);
            return true;
        }

        public bool AddVideoToFavourits(User user, long videoId)
        {
            if (!IsUserExists(user.UserID))
                throw new Exception("User not exists");

            if (pluginDBHandler.IsUserVideoExists(user.UserID, videoId))
                return false;

            var video = pluginDBHandler.GetMovieByID(videoId);

            pluginDBHandler.AddVideoToUser(user.UserID, video);
            return true;
        }

        public bool RemoveVideoFromFavourits(User user, long videoId)
        {
            if (!IsUserExists(user.UserID))
                throw new Exception("User not exists");

            if (!pluginDBHandler.IsUserVideoExists(user.UserID, videoId))
                return false;

            var video = pluginDBHandler.GetMovieByID(videoId);
            pluginDBHandler.RemoveVideoForUser(user.UserID, video);
            return true;
        }

        public long GetSeriesIdFromVideoId(int videoId)
        {
            return pluginDBHandler.GetSeriesIdFromVideoId(videoId);
        }
    }
}